import 'dart:async';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:moonplay/common/event_bus_utils.dart';
import 'package:moonplay/pages/tencentplayer/full_video_page.dart';
import 'package:moonplay/pages/tencentplayer/tencent_player_bottom_widget.dart';
import 'package:moonplay/pages/tencentplayer/tencent_player_gesture_cover.dart';
import 'package:moonplay/pages/tencentplayer/tencent_player_loading.dart';
import 'package:moonplay/pages/tencentplayer/util/common_util.dart';
import 'package:moonplay/pages/tencentplayer/util/forbidshot_util.dart';
import 'package:screen/screen.dart';
import 'package:flutter_tencentplayer/flutter_tencentplayer.dart';


class WindowVideoPage extends StatefulWidget {
  PlayType playType;
  String dataSource;
  String vodName;

  //UI
  bool showBottomWidget;
  bool showClearBtn;


  WindowVideoPage({this.showBottomWidget = true, this.showClearBtn = true, this.dataSource, this.playType = PlayType.network, this.vodName});

  @override
  _WindowVideoPageState createState() => _WindowVideoPageState();
}

class _WindowVideoPageState extends State<WindowVideoPage> {
  TencentPlayerController controller;
  VoidCallback listener;
  DeviceOrientation deviceOrientation;


  bool isLock = false;
  bool showCover = false;
  Timer timer;

  StreamSubscription _eventPauseVideo;


  _WindowVideoPageState() {
    listener = () {
      if (!mounted) {
        return;
      }
      setState(() {});
    };

  }

  @override
  void initState() {
    super.initState();
    // SystemChrome.setEnabledSystemUIOverlays([]);
    _initController();
    controller.initialize();
    controller.addListener(listener);
    hideCover();
    ForbidShotUtil.initForbid(context);
    Screen.keepOn(true);

    _eventPauseVideo = eventBus.on<PauseVideo>().listen((event) {
      controller.pause();
    });
  }

  @override
  Future dispose() {
    super.dispose();
    SystemChrome.setEnabledSystemUIOverlays([SystemUiOverlay.top, SystemUiOverlay.bottom]);
    controller.removeListener(listener);
    controller.dispose();
    ForbidShotUtil.disposeForbid();
    Screen.keepOn(false);
  }

  _initController() {
    print("播放url: ${widget.dataSource}");
    switch (widget.playType) {
      case PlayType.network:
        controller = TencentPlayerController.network(widget.dataSource);
        break;
      case PlayType.asset:
        controller = TencentPlayerController.asset(widget.dataSource);
        break;
      case PlayType.file:
        controller = TencentPlayerController.file(widget.dataSource);
        break;
      case PlayType.fileId:
        controller = TencentPlayerController.network(null, playerConfig: PlayerConfig(auth: {"appId": 1252463788, "fileId": widget.dataSource}));
        break;
    }
  }


  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: GestureDetector(
        behavior: HitTestBehavior.opaque,
        onTap: () {
          hideCover();
        },
        onDoubleTap: () {
          if (!widget.showBottomWidget || isLock) return;
          if (controller.value.isPlaying) {
            controller.pause();
          } else {
            controller.play();
          }
        },
        child:Container(
          color: Colors.black,
          height: 240,
          child: Stack(
            overflow: Overflow.visible,
            alignment: Alignment.center,
            children: <Widget>[
              /// 视频
              controller.value.initialized
                  ? AspectRatio(
                aspectRatio: controller.value.aspectRatio,
                child: TencentPlayer(controller),
              ) : Container(),
              /// 支撑全屏
              Container(),
              /// 半透明浮层
              showCover ?Container(color: Color.fromRGBO(0, 0, 0, 0.3)) : SizedBox(),
              /// 处理滑动手势
              // Offstage(
              //   offstage: isLock,
              //   child: TencentPlayerGestureCover(
              //     controller: controller,
              //     showBottomWidget: widget.showBottomWidget,
              //     behavingCallBack: delayHideCover,
              //   ),
              // ),
              /// 加载loading
              TencentPlayerLoading(controller: controller, iconW: 53,),
              /// 头部浮层
              Positioned(
                top: 0,
                left: 0,
                child: GestureDetector(
                  behavior: HitTestBehavior.opaque,
                  onTap: () {
                    Navigator.pop(context);
                  },
                  child: Container(
                    width: 50,
                    height: 50,
                    alignment: Alignment.center,
                    child: Icon(IconData(0xe632, fontFamily: Constants.IconFontFamily),color: Colors.white ,size: 20),
                  ),
                ),
              ),
              /// 锁
              // showCover ? Align(
              //   alignment: Alignment.centerLeft,
              //   child: GestureDetector(
              //     behavior: HitTestBehavior.opaque,
              //     onTap: () {
              //       setState(() {
              //         isLock = !isLock;
              //       });
              //       delayHideCover();
              //     },
              //     child: Container(
              //       color: Colors.transparent,
              //       padding: EdgeInsets.only(top: MediaQuery.of(context).padding.top, right: 20, bottom: 20, left: MediaQuery.of(context).padding.top,),
              //       child: Image.asset(isLock ? 'static/player_lock.png' : 'static/player_unlock.png', width: 38, height: 38,),
              //     ),
              //   ),
              // ): SizedBox(),
              /// 进度、清晰度、速度
              Offstage(
                offstage: !widget.showBottomWidget,
                child: Padding(
                  padding: EdgeInsets.only(left: 0, right: MediaQuery.of(context).padding.bottom),
                  // padding: EdgeInsets.only(left: MediaQuery.of(context).padding.top, right: MediaQuery.of(context).padding.bottom),
                  child: TencentPlayerBottomWidget(
                    isShow: !isLock && showCover,
                    controller: controller,
                    showClearBtn: widget.showClearBtn,
                    behavingCallBack: () {
                      delayHideCover();
                    },
                    changeClear: (int index) {
                      changeClear(index);
                    },
                    fullCallBack: (){
                      fullCallBackFun();
                    }
                  ),
                ),
              ),
              /// 全屏按钮
              // showCover ? Positioned(
              //   right: 0,
              //   bottom: 20,
              //   child: GestureDetector(
              //     behavior: HitTestBehavior.opaque,
              //     onTap: () async {
              //       TencentPlayerController newController = await Navigator.of(context).push(CupertinoPageRoute(builder: (_) => FullVideoPage(controller: controller, playType: PlayType.network), fullscreenDialog: true));
              //       setState(() {
              //         controller = newController;
              //       });
              //     },
              //     child: Container(
              //       padding: EdgeInsets.all(20),
              //       child: Image.asset('static/full_screen_on.png', width: 20, height: 20),
              //     ),
              //   ),
              // ) : SizedBox(),
            ],
          ),
        ),
      ),
    );
  }

  List<String> clearUrlList = [
    'http://1252463788.vod2.myqcloud.com/95576ef5vodtransgzp1252463788/e1ab85305285890781763144364/v.f10.mp4',
    'http://1252463788.vod2.myqcloud.com/95576ef5vodtransgzp1252463788/e1ab85305285890781763144364/v.f20.mp4',
    'http://1252463788.vod2.myqcloud.com/95576ef5vodtransgzp1252463788/e1ab85305285890781763144364/v.f30.mp4',
  ];

  changeClear(int urlIndex, {int startTime}) {
    controller?.removeListener(listener);
    controller?.pause();
    controller = TencentPlayerController.network(clearUrlList[urlIndex], playerConfig: PlayerConfig(startTime: startTime ?? controller.value.position.inSeconds));
    controller?.initialize().then((_) {
      if (mounted) setState(() {});
    });
    controller?.addListener(listener);
  }


  hideCover() {
    if (!mounted) return;
    setState(() {
      showCover = !showCover;
    });
    delayHideCover();
  }

  delayHideCover() {
    if (timer != null) {
      timer.cancel();
      timer = null;
    }
    if (showCover) {
      timer = new Timer(Duration(seconds: 6), () {
        if (!mounted) return;
        setState(() {
          showCover = false;
        });
      });
    }
  }

  fullCallBackFun() async {
    TencentPlayerController newController = await Navigator.of(context).push(CupertinoPageRoute(builder: (_) => FullVideoPage(controller: controller, playType: PlayType.network, vodName: widget.vodName), fullscreenDialog: true));
    setState(() {
      controller = newController;
    });
  }
}
